/*
+--------------------------------------------------------------------------
|   Mtons [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
+---------------------------------------------------------------------------
*/
package com.mtons.mblog.web.formatter;

import com.mtons.mblog.base.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.beans.PropertyEditorSupport;
import java.util.regex.Pattern;

/**
 * Created by langhsu on 2017/9/2.
 */
@Slf4j
public class StringToDateEditor extends PropertyEditorSupport {
    public StringToDateEditor() {
        super();
    }
    @Override
    public String getAsText() {
        Object value = getValue();
        return value != null ? value.toString() : "";
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (text == null) {
            setValue(null);
        } else {
            String value = text;
            final String[] split = value.split("-");
            if(split.length == 3){
                final String[] timeSplits = split[2].split(":");
                if(timeSplits.length == 3){
                    setValue(CommonUtils.getStrTime(text,"yyyy-MM-dd HH:mm:ss"));
                }else {
                    setValue(CommonUtils.getStrTime(text,"yyyy-MM-dd HH:mm"));
                }
            }else {
                setValue(value);
            }

        }
    }
}
